const fp = require('lodash/fp');
class Container {
  static of (value) {
    return new Container(value)
  }
  constructor (value) {
    this._value = value
  }
  map (fn) {
    return Container.of(fn(this._value))
  }
}

class MayBe {
  static of (x) {
    return new MayBe(x)
  }
  isNothing () {
    return this._value === null || this._value === undefined
  }
  constructor (x) {
    this._value = x
  }
  map (fn) {
    return this.isNothing() ? this : MayBe.of(fn(this._value))
  }
}
// 练习1
let maybe = MayBe.of([5, 6, 1])
let ex1 = (value) => {
  return maybe.map(fp.map(fp.add(value)))
}

// 练习2
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
let ex2 = () => {
  return xs.map(fp.first)._value
}

// 练习3
let safeProp = fp.curry((x, o) => {
  return MayBe.of(o[x])
})
let user = { id: 2, name: 'Albert' }
let ex3 = () => {
  return safeProp('name', user).map(fp.first)._value
}

let ex4 = (n) => {
  let mayBe = new MayBe(n)
  let value = mayBe.map(parseInt)
  return value._value
}